// spfa是对bellman算法的一个改进
// 这是由于只有其入点的最小值跟新了之后，才需要继续更新下一个最小值
// 用队列来维护
// 时间复杂度：O(m)

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

const int N = 100010, M = 200010;
int n, m;
queue<int> q;
int h[N], e[M], ne[M], w[M], dist[N], idx;
// st数组用来存储每个节点是否在队列中
bool st[N];
// dist数组用来存储最短距离

void add(int a, int b, int c)
{
    e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}

int spfa()
{
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0, st[1] = true;
    q.push(1);
    while (q.size())
    {
        int t = q.front();
        int distance = dist[t];
        q.pop();
        st[t] = false;
        for (int i = h[t]; ~i; i = ne[i])
        {
            int j = e[i];
            // cout << j << endl;
            if (dist[j] > distance + w[i])
            {
                dist[j] = distance + w[i];
                // cout << w[j] << endl;
                if (!st[j])
                {
                    q.push(j);
                    st[j] = true;
                }
            }
        }
    }
    return dist[n];
}

int main()
{
    memset(h, -1, sizeof h);
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for (int i = 0; i < m; ++i)
    {
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c);
    }
    // cout << w[0] << ' ' << w[1] << ' ' << w[2] << endl;
    int ret = spfa();
    if (ret > 0x3f3f3f3f / 2)
        cout << "impossible\n";
    else
        cout << ret << endl;
    return 0;
}
